import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 消息管理/模板服务映射
 * @date: 2018-8-21


 */
import { queryIdpValue } from 'cpaas-front/lib/services/api';
import { getResponse, createPagination } from 'utils/utils';
import { querySMSList, searchHeader, saveHeader as _saveHeader, updateHeader as _updateHeader, deleteLine as _deleteLine, deleteHeader as _deleteHeader, fetchLangType as _fetchLangType, sendMessage as _sendMessage, getParams as _getParams, searchCopyHeader, fetchTemplateData as _fetchTemplateData, GetWebhook, deleteWebhook as _deleteWebhook, createWebhook as _createWebhook } from "../services/sendConfigService";
import { searchCategoryCodeTree } from "../services/messageTemplateService";
export default {
  namespace: 'sendConfig',
  state: {
    list: [],
    // 数据列表
    pagination: {},
    // 分页器
    header: {},
    // 审核规则头
    messageType: [],
    // 消息类型
    langType: [],
    // 语言类型
    paramsName: [],
    // 参数名称
    copyDetail: {},
    templateData: {},
    // 模板数据
    categoryTree: [],
    webHook: {}
  },
  effects: {
    fetchType: /*#__PURE__*/_regeneratorRuntime.mark(function fetchType(_, _ref) {
      var call, put, categoryTree;
      return _regeneratorRuntime.wrap(function fetchType$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.t0 = getResponse;
              _context.next = 4;
              return call(searchCategoryCodeTree, {
                'CMSG.TEMP_SERVER.CATEGORY': 1,
                'CMSG.TEMP_SERVER.SUBCATEGORY': 2
              });

            case 4:
              _context.t1 = _context.sent;
              categoryTree = (0, _context.t0)(_context.t1);
              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  categoryTree: categoryTree
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, fetchType);
    }),
    // 获取表格数据
    queryTableList: /*#__PURE__*/_regeneratorRuntime.mark(function queryTableList(_ref2, _ref3) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function queryTableList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(querySMSList, payload);

            case 4:
              result = _context2.sent;
              result = getResponse(result);

              if (!result) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  list: result.content,
                  pagination: createPagination(result)
                }
              });

            case 9:
              return _context2.abrupt("return", result);

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, queryTableList);
    }),
    // 获取消息类型
    queryMessageType: /*#__PURE__*/_regeneratorRuntime.mark(function queryMessageType(_, _ref4) {
      var call, put, messageType;
      return _regeneratorRuntime.wrap(function queryMessageType$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              call = _ref4.call, put = _ref4.put;
              _context3.t0 = getResponse;
              _context3.next = 4;
              return call(queryIdpValue, 'CMSG.MESSAGE_TYPE');

            case 4:
              _context3.t1 = _context3.sent;
              messageType = (0, _context3.t0)(_context3.t1);
              _context3.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  messageType: messageType
                }
              });

            case 8:
            case "end":
              return _context3.stop();
          }
        }
      }, queryMessageType);
    }),
    // 获取语言类型
    fetchLangType: /*#__PURE__*/_regeneratorRuntime.mark(function fetchLangType(_ref5, _ref6) {
      var payload, call, put, list, result;
      return _regeneratorRuntime.wrap(function fetchLangType$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call, put = _ref6.put;
              _context4.next = 4;
              return call(_fetchLangType, payload);

            case 4:
              list = _context4.sent;
              result = getResponse(list);

              if (!result) {
                _context4.next = 9;
                break;
              }

              _context4.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  langType: result
                }
              });

            case 9:
            case "end":
              return _context4.stop();
          }
        }
      }, fetchLangType);
    }),
    // 改变语言获取参数
    getParams: /*#__PURE__*/_regeneratorRuntime.mark(function getParams(_ref7, _ref8) {
      var payload, call, put, list, result;
      return _regeneratorRuntime.wrap(function getParams$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call, put = _ref8.put;
              _context5.next = 4;
              return call(_getParams, payload);

            case 4:
              list = _context5.sent;
              result = getResponse(list);

              if (!result) {
                _context5.next = 9;
                break;
              }

              _context5.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  paramsName: result
                }
              });

            case 9:
            case "end":
              return _context5.stop();
          }
        }
      }, getParams);
    }),
    // 获取头信息
    fetchHeader: /*#__PURE__*/_regeneratorRuntime.mark(function fetchHeader(_ref9, _ref10) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchHeader$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call, put = _ref10.put;
              _context6.next = 4;
              return call(searchHeader, _objectSpread({}, payload));

            case 4:
              result = _context6.sent;
              result = getResponse(result);

              if (!result) {
                _context6.next = 9;
                break;
              }

              _context6.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  header: result
                }
              });

            case 9:
            case "end":
              return _context6.stop();
          }
        }
      }, fetchHeader);
    }),
    // 获取头信息
    fetchCopyHeader: /*#__PURE__*/_regeneratorRuntime.mark(function fetchCopyHeader(_ref11, _ref12) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchCopyHeader$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call, put = _ref12.put;
              _context7.next = 4;
              return call(searchCopyHeader, _objectSpread({}, payload));

            case 4:
              result = _context7.sent;
              result = getResponse(result);

              if (!result) {
                _context7.next = 9;
                break;
              }

              _context7.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  copyDetail: result
                }
              });

            case 9:
              return _context7.abrupt("return", result);

            case 10:
            case "end":
              return _context7.stop();
          }
        }
      }, fetchCopyHeader);
    }),
    // 获取信息
    getWebhook: /*#__PURE__*/_regeneratorRuntime.mark(function getWebhook(_ref13, _ref14) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function getWebhook$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref13.payload;
              call = _ref14.call, put = _ref14.put;
              _context8.next = 4;
              return call(GetWebhook, _objectSpread({}, payload));

            case 4:
              result = _context8.sent;
              result = getResponse(result);

              if (!result) {
                _context8.next = 9;
                break;
              }

              _context8.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  webHook: {
                    webHookData: result.content,
                    pagination: createPagination(result)
                  }
                }
              });

            case 9:
            case "end":
              return _context8.stop();
          }
        }
      }, getWebhook);
    }),
    // 发送消息
    sendMessage: /*#__PURE__*/_regeneratorRuntime.mark(function sendMessage(_ref15, _ref16) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function sendMessage$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref15.payload;
              call = _ref16.call;
              _context9.next = 4;
              return call(_sendMessage, _objectSpread({}, payload));

            case 4:
              result = _context9.sent;
              return _context9.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, sendMessage);
    }),
    // 新增模板服务
    saveHeader: /*#__PURE__*/_regeneratorRuntime.mark(function saveHeader(_ref17, _ref18) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function saveHeader$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              payload = _ref17.payload;
              call = _ref18.call;
              _context10.next = 4;
              return call(_saveHeader, _objectSpread({}, payload));

            case 4:
              result = _context10.sent;
              return _context10.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context10.stop();
          }
        }
      }, saveHeader);
    }),
    // 修改模板服务
    updateHeader: /*#__PURE__*/_regeneratorRuntime.mark(function updateHeader(_ref19, _ref20) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function updateHeader$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              payload = _ref19.payload;
              call = _ref20.call;
              _context11.next = 4;
              return call(_updateHeader, _objectSpread({}, payload));

            case 4:
              result = _context11.sent;
              return _context11.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context11.stop();
          }
        }
      }, updateHeader);
    }),
    // 删除模板服务
    deleteHeader: /*#__PURE__*/_regeneratorRuntime.mark(function deleteHeader(_ref21, _ref22) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteHeader$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              payload = _ref21.payload;
              call = _ref22.call;
              _context12.next = 4;
              return call(_deleteHeader, _objectSpread({}, payload));

            case 4:
              result = _context12.sent;
              return _context12.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context12.stop();
          }
        }
      }, deleteHeader);
    }),
    // 删除行服务
    deleteLine: /*#__PURE__*/_regeneratorRuntime.mark(function deleteLine(_ref23, _ref24) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteLine$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              payload = _ref23.payload;
              call = _ref24.call;
              _context13.next = 4;
              return call(_deleteLine, _objectSpread({}, payload));

            case 4:
              result = _context13.sent;
              return _context13.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context13.stop();
          }
        }
      }, deleteLine);
    }),
    // 删除
    deleteWebhook: /*#__PURE__*/_regeneratorRuntime.mark(function deleteWebhook(_ref25, _ref26) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteWebhook$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              payload = _ref25.payload;
              call = _ref26.call;
              _context14.next = 4;
              return call(_deleteWebhook, _objectSpread({}, payload));

            case 4:
              result = _context14.sent;
              return _context14.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context14.stop();
          }
        }
      }, deleteWebhook);
    }),
    //
    createWebhook: /*#__PURE__*/_regeneratorRuntime.mark(function createWebhook(_ref27, _ref28) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function createWebhook$(_context15) {
        while (1) {
          switch (_context15.prev = _context15.next) {
            case 0:
              payload = _ref27.payload;
              call = _ref28.call;
              _context15.next = 4;
              return call(_createWebhook, _objectSpread({}, payload));

            case 4:
              result = _context15.sent;
              return _context15.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context15.stop();
          }
        }
      }, createWebhook);
    }),
    fetchTemplateData: /*#__PURE__*/_regeneratorRuntime.mark(function fetchTemplateData(_ref29, _ref30) {
      var payload, call, put, res, result;
      return _regeneratorRuntime.wrap(function fetchTemplateData$(_context16) {
        while (1) {
          switch (_context16.prev = _context16.next) {
            case 0:
              payload = _ref29.payload;
              call = _ref30.call, put = _ref30.put;
              _context16.next = 4;
              return put({
                type: 'updateState',
                payload: {
                  templateData: ''
                }
              });

            case 4:
              _context16.next = 6;
              return call(_fetchTemplateData, _objectSpread({}, payload));

            case 6:
              res = _context16.sent;
              result = getResponse(res);

              if (!result) {
                _context16.next = 11;
                break;
              }

              _context16.next = 11;
              return put({
                type: 'updateState',
                payload: {
                  templateData: result.templateContent
                }
              });

            case 11:
            case "end":
              return _context16.stop();
          }
        }
      }, fetchTemplateData);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};